package com.luckmgr.system;

import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;

import com.alibaba.fastjson.JSON;
import com.common.utils.DateUtil;
import com.luckmgr.dto.ActivityDTO;
import com.luckmgr.entity.db.LuckRecord;
import com.luckmgr.entity.db.Message;
import com.luckmgr.vo.LottoRecordVO;

public class SystemData {
	
	static public final Integer MAX_LOTTO_LIST_SIZE=60;
	
	static public ConcurrentMap<String, ActivityDTO> activitys = new ConcurrentHashMap<String, ActivityDTO>();
	
	static public ConcurrentMap<String, Queue<LottoRecordVO>> lottoRecords = new ConcurrentHashMap<String,Queue<LottoRecordVO>>();
	
	static public ConcurrentMap<String,Message> messages = new ConcurrentHashMap<String,Message>();
	
	
	public static void addMessage(Message message){
		messages.put(message.getSeriesCode(), message);
	}
	
	public static Message getMessage(String seriesCode){
		return messages.get(seriesCode);
	}
	
	public static boolean ishaveActivity(String acode){
		if(activitys.containsKey(acode)){
			return true;
		}else{
			return false;
		}
	}
	
	
	public static ActivityDTO findActivityDTO(String acode){
		return activitys.get(acode);
	}
	
	public static synchronized void addLottoRecord(String acode,LuckRecord luckRecord){
		Queue<LottoRecordVO> queue = lottoRecords.get(acode);
		if(null == queue){
			queue = new ConcurrentLinkedQueue<LottoRecordVO>();
			lottoRecords.put(acode, queue);
		}
		while (queue.size()>=MAX_LOTTO_LIST_SIZE) {
			queue.poll();
		}
		
		if(luckRecord.getUserName().length()>3){
			LottoRecordVO lottoRecordVO = new LottoRecordVO();
			lottoRecordVO.setUserName(luckRecord.getUserName().substring(0, luckRecord.getUserName().length() - 3)+"***");
			lottoRecordVO.setPname(luckRecord.getPrizeName());
			lottoRecordVO.setTime(DateUtil.format(luckRecord.getPrizeTime(), "yyyy-MM-dd"));
			queue.add(lottoRecordVO);
		}
	}
	
	public static synchronized String findLottoRecord(String acode){
		Queue<LottoRecordVO> queue = lottoRecords.get(acode);
		if(null != queue){
			return JSON.toJSONString(queue);
		}
		return "";
	}
	
}
